package com.demo.LocalDateTime;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateTest {

    public void test() {
        // 时间戳  1970年1月1日00：00：00 到某一个时间点的毫秒值
        // 默认获取UTC时区
        Instant ins = Instant.now();
        System.out.println(ins);
        System.out.println(Instant.now().atOffset(ZoneOffset.ofHours(8)));

        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        System.out.println(System.currentTimeMillis());

        System.out.println(Instant.now().toEpochMilli());
        System.out.println(Instant.now().atOffset(ZoneOffset.ofHours(8)).toInstant().toEpochMilli());

    }


    public void test1() {
        //自定义下一个工作日
        LocalDateTime ldt = LocalDateTime.now();
        LocalDateTime with = ldt.with((x) -> {
            LocalDateTime ldt1 = (LocalDateTime) x;
            DayOfWeek dayOfWeek = ldt1.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                return ldt1.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                return ldt1.plusDays(2);
            } else {
                return ldt1.plusDays(1);
            }
        });
        System.out.println(with);
    }


    public void test4() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        System.out.println(format);

        Date parse = null;
        try {
            parse = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(parse);
    }


    public void test5() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();

        String format = dtf.format(localDateTime);
        System.out.println(format);


        LocalDateTime parse = LocalDateTime.parse(format, dtf);
        System.out.println(parse);


    }


    public void test7() {
        LocalDateTime localDateTime = LocalDateTime.of(2020, 02, 29, 10, 10, 10);

        Duration duration = Duration.ofSeconds(10);
        System.out.println(duration.getSeconds());
        System.out.println(localDateTime.withYear(2019));
    }
}
